//
// Created by syj on 2024/3/14.
//

#ifndef TRANSMIDDLE_ASYNC_H
#define TRANSMIDDLE_ASYNC_H

#include <spdlog/async_logger.h>
#include <spdlog/details/registry.h>
#include <spdlog/details/thread_pool.h>

#include <functional>
#include <memory>
#include <mutex>

namespace log{
    namespace details{
        static const size_t default_async_q_size = 8192;
    }

    // async logger factory - creates async loggers backed with thread pool.
// if a global thread pool doesn't already exist, create it with default queue
// size of 8192 items and single thread.
    template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
    struct async_factory_impl
    {
        template<typename It>
        static std::shared_ptr<async_logger> create(std::string logger_name, It begin, It end)
        {
            auto &registry_inst = spdlog::details::registry::instance();

            // create global thread pool if not already exists..

            auto &mutex = registry_inst.tp_mutex();
            std::lock_guard<std::recursive_mutex> tp_lock(mutex);
            auto tp = registry_inst.get_tp();
            if (tp == nullptr)
            {
                tp = std::make_shared<spdlog::details::thread_pool>
                        (log::details::default_async_q_size, 1U);
                registry_inst.set_tp(tp);
            }

            auto new_logger = std::make_shared<spdlog::async_logger>(
                    std::move(logger_name),
                    std::move(begin),
                    std::move(end),
                    std::move(tp),
                    OverflowPolicy);
            registry_inst.initialize_logger(new_logger);
            return new_logger;
        }
    };

    using async_factory = async_factory_impl<async_overflow_policy::block>;
    using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;

    template<typename It>
    inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, It begin, It end)
    {
        return async_factory::create<It>(std::move(logger_name), std::move(begin), std::move(end));
    }

    template<typename It>
    inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, It begin, It end)
    {
        return async_factory_nonblock::create<It>(std::move(logger_name), std::move(begin), std::move(end));
    }

// set global thread pool.
    inline void init_thread_pool(
            size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
    {
        auto tp = std::make_shared<spdlog::details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
        spdlog::details::registry::instance().set_tp(std::move(tp));
    }

    inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
    {
        init_thread_pool(q_size, thread_count, on_thread_start, [] {});
    }

    inline void init_thread_pool(size_t q_size, size_t thread_count)
    {
        init_thread_pool(
                q_size, thread_count, [] {}, [] {});
    }

// get the global thread pool.
    inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
    {
        return spdlog::details::registry::instance().get_tp();
    }
}

#endif //TRANSMIDDLE_ASYNC_H
